//
//  MainViewController.swift
//  微博
//
//  Created by 付涛 on 15/7/27.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
         //加载子控制器
        addChildViewControllers()
        
    }
    
    
    override func viewDidAppear(animated: Bool) {
        
//       print(tabBar.subviews)
        
         setUpComposeButton()
        
    }
    
//    设置自定义按钮的位置

    func setUpComposeButton(){
        
            //print(composeButton)
     let width = tabBar.bounds.width / CGFloat(childViewControllers.count)
        
        let rect = CGRect(x: 0, y: 0, width: width, height: tabBar.bounds.height)
        
        composeButton.frame = CGRectOffset(rect, width * 2, 0)
        
        
    }
    
//    按钮点击
    func clickComposeButton() {
    
      //print(__FUNCTION__)
        
    print("fd")
    
    }
    

    
    private func addChildViewControllers() {
        
        
        addChildViewControlle(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        
        addChildViewControlle(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewControlle(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewControlle(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
        
    }
    
    
    
    private func addChildViewControlle (vc: UIViewController,title:String,imageName:String ) {
        
        let vc = vc
        
        tabBar.tintColor = UIColor.orangeColor()
        
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
        

        
    }
    

//    懒加载子控件
    
    lazy private var composeButton: UIButton = {
    
       // let btn = UIButton(type: UIButtonType.ContactAdd)
        let btn = UIButton()
        
       btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState:UIControlState.Normal)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_hightlighted"), forState: UIControlState.Highlighted)
        
        self.tabBar.addSubview(btn)
        
        
        btn.addTarget(self, action: "clickComposeButton", forControlEvents:UIControlEvents.TouchUpInside)
        
    return btn
        
    }()
    
    
}
